//
//  SetUpViewController.m
//  HeSaid
//
//  Created by MWJ on 15/5/4.
//  Copyright (c) 2015年 MWJ. All rights reserved.
//

#import "SetUpViewController.h"
#import "AboutViewController.h" //“关于app”界面
#import "VersionUpdateViewController.h" //"版本更新"界面
#import "ModifyDataViewControlle.h" //“修改资料”界面
#import "feedbackViewController.h" //“意见调查”界面
#import "friendViewController.h" //“好友查看”界面
#import "TextViewController.h"  //“意见反馈”界面
#import "AppDelegate.h"
#import "PersonViewController.h"


@interface SetUpViewController (){
    AppDelegate *app;
    UIAlertView *_alert;
}

@end

@implementation SetUpViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.logo.layer.masksToBounds = YES;
    self.logo.layer.cornerRadius = self.logo.bounds.size.width/2;
    app=(AppDelegate *)[[UIApplication sharedApplication]delegate];
    
    app.defaults = [NSUserDefaults standardUserDefaults];
    app.feedbackstr = [app.defaults objectForKey:@"feedBack"];

    //tableView 顶端不出现空白
    self.automaticallyAdjustsScrollViewInsets=NO;
    
    //tableView 不可滑动
    self.SetUpTableView.scrollEnabled=NO;
    
    //tableview设置行间隔边框
    self.SetUpTableView.separatorColor = [UIColor blackColor];//设置行间隔边框
    
//    self.navigationItem.hidesBackButton=YES;
    self.labeltext.numberOfLines=0;
    self.labeltext.lineBreakMode=NSLineBreakByCharWrapping;
    self.labeltext.text=@"我们已经收到您的宝贵意见！\n请耐心等待更新！\n谢谢！！";
    
    
}

#pragma mark - tableview的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 6;
}

#pragma mark - tableview的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

#pragma mark - tableview的内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIdentifier=@"cell";
    
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    NSArray *array=[[NSArray alloc]initWithObjects:@"关于App",@"版本更新",@"好友查看",@"修改资料",@"清除缓存",@"举报反馈", nil];
    
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.textLabel.text=array[indexPath.row];
    
    return cell;
}


#pragma mark - cell 的点击事件

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //“关于app” 跳转
    if (indexPath.row == 0) {
        AboutViewController *aboutVC=[[AboutViewController alloc]initWithNibName:@"AboutViewController" bundle:nil];
        [self.navigationController pushViewController:aboutVC animated:YES];
    }
    //“版本更新” 跳转
    if (indexPath.row == 1) {
        VersionUpdateViewController *versionUpdate=[[VersionUpdateViewController alloc]initWithNibName:@"VersionUpdateViewController" bundle:nil];
        [self.navigationController pushViewController:versionUpdate animated:YES];
    }
    //“好友查看” 跳转
    if (indexPath.row == 2) {
        friendViewController *friend=[[friendViewController alloc]initWithNibName:@"friendViewController" bundle:nil];
        [self.navigationController pushViewController:friend animated:YES];
    }
    //“修改资料” 跳转
    if (indexPath.row == 3) {
        ModifyDataViewControlle *modifyData=[[ModifyDataViewControlle alloc]initWithNibName:@"ModifyDataViewControlle" bundle:nil];
        [self.navigationController pushViewController:modifyData animated:YES];
    }
    //“清除缓存”
    if (indexPath.row == 4) {
        dispatch_async(
                       dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
                       , ^{
                           NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) objectAtIndex:0];
                           
                           NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
                           for (NSString *p in files) {
                               NSError *error;
                               NSString *path = [cachPath stringByAppendingPathComponent:p];
                               if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
                                   [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
                               }
                           }
                           [self performSelectorOnMainThread:@selector(clearCacheSuccess) withObject:nil waitUntilDone:YES];});
    }
//    
//    
//    //“意见调查” 跳转
//    if (indexPath.row == 5) {
//        if ([app.feedbackstr isEqualToString:@"1"]) {
//            self.tsview.hidden = NO;
//        }else{
//        feedbackViewController *feedback=[[feedbackViewController alloc]initWithNibName:@"feedbackViewController" bundle:nil];
//        [self.navigationController pushViewController:feedback animated:YES];
//        }
//    }
    //“举报反馈” 跳转
    if (indexPath.row == 5) {
        TextViewController *textback=[[TextViewController alloc]initWithNibName:@"TextViewController" bundle:nil];
        [self.navigationController pushViewController:textback animated:YES];
    }
    
}

-(void)clearCacheSuccess
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"缓存清除成功" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    _alert=alert;
    [alert show];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

- (IBAction)gb:(id)sender {
    self.tsview.hidden = YES;
}
@end
